package com.dhq.myapp.base

import android.content.Intent
import android.content.pm.ActivityInfo
import android.graphics.Color.TRANSPARENT
import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.view.WindowManager
import android.widget.LinearLayout
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import com.dhq.myapp.R
import com.dhq.myapp.presenter.base.MvpPresenter
import com.dhq.myapp.util.HeaderUtil
import com.hjq.base.action.KeyboardAction

/**
 * DESC
 * Created by DHQ on 2025/10/12.
 */
abstract class BaseActivity : AppCompatActivity() , KeyboardAction {

    private lateinit var headerUtil: HeaderUtil
    private lateinit var mvpPresenterImls: ArrayList<MvpPresenter<*>>

//    val TAG = this.localClassName


    override fun onCreate(savedInstanceState: Bundle?) {
        // 设置为竖屏
        if (requestedOrientation != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
            requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        }
        super.onCreate(savedInstanceState)
        val contentView = layoutInflater.inflate(R.layout.activity_base, null)
        setContentView(contentView)

        val contentLinear = findViewById<LinearLayout>(R.id.content_view)
        contentLinear.addView(
            View.inflate(this, getLayoutId(), null),
            LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)
        )

        headerUtil = HeaderUtil(this, contentView)

        initStatusBar()
        enableEdgeToEdge()
        initActivity()
    }

    protected fun initStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            val window = window
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS or WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION)
            window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            window.statusBarColor = TRANSPARENT
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // 设置透明状态栏,这样才能让 ContentView 向上
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        }
    }

    protected open fun initActivity() {
//        initLayout()
        initView()
        initData()
    }

    /**
     * 初始化布局
     */
//    protected open fun initLayout() {
//        if (getLayoutId() > 0) {
////            setContentView(getLayoutId())
//            initSoftKeyboard()
//        }
//    }


    /**
     * 初始化软键盘
     */
    protected open fun initSoftKeyboard() {
        // 点击外部隐藏软键盘，提升用户体验
        getContentView()?.setOnClickListener {
            // 隐藏软键，避免内存泄漏
            hideKeyboard(currentFocus)
        }
    }


    override fun onDestroy() {
        super.onDestroy()
        cleanPresenter()
//        removeCallbacks()
    }

    override fun finish() {
        super.finish()
        // 隐藏软键，避免内存泄漏
        hideKeyboard(currentFocus)
    }

    /**
     * 如果当前的 Activity（singleTop 启动模式） 被复用时会回调
     */
    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        // 设置为当前的 Intent，避免 Activity 被杀死后重启 Intent 还是最原先的那个
        setIntent(intent)
    }

//    override fun getBundle(): Bundle? {
//        return intent.extras
//    }

    /**
     * 和 setContentView 对应的方法
     */
    open fun getContentView(): ViewGroup? {
        return findViewById(Window.ID_ANDROID_CONTENT)
    }


    fun addPresenter(presenterIml: MvpPresenter<*>) {
        if (mvpPresenterImls == null) {
            mvpPresenterImls = ArrayList<MvpPresenter<*>>()
        }
        mvpPresenterImls.add(presenterIml)
    }

    fun cleanPresenter() {
//        if (mvpPresenterImls != null && !mvpPresenterImls.isEmpty()) {
//            val size: Int = mvpPresenterImls.size
//            for (i in 0..<size) {
//                val mvpPresenterIml: MvpPresenter<*> = mvpPresenterImls.get(i)
//
//                mvpPresenterIml.detachView(false)
//            }
//        }
    }


//    override fun getContext(): Context {
//        return this
//    }

    /**
     * 获取布局 ID
     */
    protected abstract fun getLayoutId(): Int

    /**
     * 初始化控件
     */
    protected abstract fun initView()

    /**
     * 初始化数据
     */
    protected abstract fun initData()



    fun getHeaderUtil(): HeaderUtil {
        return headerUtil
    }

    protected fun toast(msg:String){
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
    }


    interface OnActivityCallback {

        /**
         * 结果回调
         *
         * @param resultCode        结果码
         * @param data              数据
         */
        fun onActivityResult(resultCode: Int, data: Intent?)
    }




}